package org.gnyxy.controller.front;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.gnyxy.constant.StatusConstant;
import org.gnyxy.entity.Dish;
import org.gnyxy.result.Result;
import org.gnyxy.service.DishService;
import org.gnyxy.vo.DishVO;
import org.gnyxy.vo.DishWithInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("UserDishController")
@RequestMapping("/front/dish")
@Slf4j
@Api(tags = "小程序端-菜品")
public class DishController {

    @Autowired
    private DishService dishService;

    /**
     * 菜品分类查询
     * @param typeId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("分类查询菜品")
    @Cacheable(cacheNames = "dishCache",key = "#typeId")
    public Result<List<DishVO>> list(Long typeId){
        log.info("分类查询菜品id为：{}",typeId);
        Dish dish = Dish.builder()
                .typeId(typeId)
                .status(StatusConstant.ENABLE)
                .build();

         List<DishVO> dishes = dishService.getByTypeIdAndStatus(dish);
         return Result.success(dishes);
    }

    /**
     * 根据菜名搜索菜品
     * @param name
     * @return
     */
    @GetMapping("/{name}")
    @ApiOperation("根据菜名搜索菜品")
    public Result<List<DishVO>> getDishByName(@PathVariable String name){
        log.info("根据菜名: {} 搜索商品,",name);
        List<DishVO> dishVOS = dishService.getDishByName(name);
        return Result.success(dishVOS);
    }

    /**
     * 查询所有菜品
     * @return
     */
    @GetMapping("/getAll")
    @ApiOperation("查询所有菜品")
    @Cacheable(cacheNames = "dishCache", key = "'AllDishes'")
    public Result<List<DishVO>> getAll(){
        log.info("查询所有商品");
        List<DishVO> dishVOS = dishService.getAll();
        return Result.success(dishVOS);
    }

    /**
     * 根据id查看商品详情
     * @param id
     * @return
     */
    @GetMapping("/getInfos/{id}")
    @ApiOperation("根据id查看商品详情")
    @Cacheable(cacheNames = "dishInfoCache",key = "#id")
    public Result<DishWithInfoVO> getDishAndInfo(@PathVariable Long id){
        log.info("根据id: {}查看商品详情",id);
        DishWithInfoVO dishWithInfoVO = dishService.getDishAndInfo(id);
        return Result.success(dishWithInfoVO);
    }


}
